<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['basic']=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
    $member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','groupid','avatar','nickname'));
	if(!member){
		exi('找不到会员信息！','error');
	}
	$user_id=pdo_getcolumn('jy_vote_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');
	if(!$user_id){
		$insert=array(
			'mid'=>$_SESSION['uid'],
			'uniacid'=>$_SESSION['uniacid'],
			//'mobile'=>$member['mobile'],
			'nickname'=>$member['nickname'],
			'avatar'=>$member['avatar'],
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_vote_user',$insert);
	}
	$user=pdo_get('jy_vote_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$return['member']=$member;
	$return['user']=$user;
	$return['item']=array();
	exi($return);
}
//修改用户信息
if($_RQ['op']=='set'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$data=array(
			'nickname'=>$postdata['nickname'],
			'avatar'=>$postdata['avatar'],
			'description'=>$postdata['description'],
		);
		$user_id=pdo_getcolumn('jy_vote_user',array('mid'=>$_SESSION['uid']),'id');		
		if($user_id){
			pdo_update('jy_vote_user',$data,array('id'=>$_SESSION['uid']));
			exi('修改成功','','jy_vote/home/');		 
		}
	}
	$user=pdo_get('jy_vote_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$return['item']=$user;
	$return['member']=$member;
	exi($return);
	}
//购买会员
if($_RQ['op']=='buyvip'){
	$post=array();
	$post['paytype']=1;
	$post['num']=1;
	if($_RQ['postdata']){
		$postdata=get_postdata();
		//print_r($postdata);exit;
		$packnum=$postdata['num'];
		$packname=$postdata['paytype'];
		$basic=$this->plugin_setting('basic');
		if($packname==1){
			$fee=$basic['mfee']*$packnum;
		}elseif($packname==2){
			$fee=$basic['jfee']*$packnum;	
		}elseif($packname==3){
			$fee=$basic['yfee']*$packnum;
		}
		//创建一个会员订单
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'packnum'=>$packnum,
			'ordersn'=>get_ordersn('jy_vote_viporder','ordersn'),
			'fee'=>$fee,
			'packname'=>$packname,
			'ispay'=>0,
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_vote_viporder',$insert);
		$id=pdo_insertid();
		$params['fee']=$fee;
		$params['title']='购买会员订单';
		$params['id']=$id;
		$params['plugin']='jy_vote';
		$params['back_url']='jy_vote/home/payresult';
		cfc('core')->pay($params);
	}
	$return['post']=$post;
	$basic=$this->plugin_setting('basic');
	$return['vipbg']=$basic['vipbg'];
	exi($return);
}

if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['payresult'];unset($_SESSION['payresult']);
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_vote_viporder',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！购买会员订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！购买会员订单金额不匹配！','error');
	}
	pdo_update('jy_vote_viporder',array('status'=>1,'ispay'=>1,'fee'=>$payresult['fee']),array('id'=>$order['id']));
	//计算到期时间
	$startTime =time();
	$user=pdo_get('jy_vote_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$order['mid']));
	if($order['packname']==3){
		$order['packnum']=$order['packnum']*12;
	}elseif($order['packname']==2){
		$order['packnum']=$order['packnum']*3;
	}
	if($user['viptime']!=0){
		
		$addTime=$user['viptime']+$order['packnum']*30*24*3600;
	}else{
		$addTime =$startTime+$order['packnum']*30*24*3600;
	}
	pdo_update('jy_vote_user',array('isvip'=>1,'viptime'=>$addTime),array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
	exi('支付成功','success','jy_vote/home/main/');
}

//我发出的红包
if($_RQ['op']=='fred'){
	$list=pdo_getall('jy_vote_redorder',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1));
	if($list){
		foreach($list as &$item){
			//收红包人信息
			$suser=pdo_get('jy_vote_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['omid']));
			$item['suser']=$suser;
			//作品信息
			$opus=pdo_get('jy_vote_opus',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['oid']));
			$item['opus']=$opus;
		}
	}
	$return['list']=$list;
	exi($return);
}

//我收到的红包
if($_RQ['op']=='sred'){
	$list=pdo_getall('jy_vote_redorder',array('uniacid'=>$_SESSION['uniacid'],'omid'=>$_SESSION['uid'],'status'=>1));
	if($list){
		foreach($list as &$item){
			//发红包人信息
			$fuser=pdo_get('jy_vote_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			$item['fuser']=$fuser;
		}
	}
	$return['list']=$list;
	exi($return);
}
//我的提现记录
if($_RQ['op']=='cashrecord'){
	$params=array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['status']=$_RQ['status'];
	}
	$list=pdo_getall('jy_vote_cashrecord',$params);
	if($list){
		foreach($list as &$item){
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$item['overtime']=date('Y-m-d H:i:s',$item['overtime']);
		}
	}
	$return['list']=$list;
	exi($return);
}

//申请提现
if($_RQ['op']=='tixian'){
	$post=array();
	$post['money']=0;
	 $user=pdo_get('jy_vote_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if($_RQ['postdata']){
	   $postdata=get_postdata();
	   $money=$postdata['money'];
	   //被拉黑用户无法提现
	   if($user['status']==1){
		   exi('您已进入黑名单,无法提现，请联系管理员','error');
	   }
	   if(!$money||$money<=0){
		   exi('提现金额不规范，请重新填写提现金额','error');
	   }
	   if($money>$user['cash']){
		   exi('提现金额不得大于可提现金额，请重新填写提现金额','error');
	   }
	   //创建一个状态为正在申请的提现记录
	   $insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'money'=>$money,
			'status'=>0,
			'ordersn'=>get_ordersn('jy_vote_cashrecord','ordersn'),
			'createtime'=>TIMESTAMP
	   );
	   pdo_insert('jy_vote_cashrecord',$insert);
	   //修改用户可提现金额
	   pdo_update('jy_vote_user',array('cash'=>$user['cash']-$money),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	   exi('申请提现成功！','success','jy_vote/home/cashrecord/');
	
}
     $return['post']=$post;
     $return['user']=$user;
	 exi($return);
}